package com.situ.blog.dao.impl;

import com.situ.blog.dao.IAdminDao;
import com.situ.blog.pojo.Admin;
import com.situ.blog.pojo.query.AdminQuery;
import com.situ.blog.util.JDBCUtil;
import com.situ.blog.util.JDBCUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AdminDaoImpl implements IAdminDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Admin> selectByPage(AdminQuery adminQuery) {
        String sql = "SELECT id,name,password,role,image,status,is_deleted,create_time,update_time FROM admin WHERE 1=1";
        List<Object> queryList = new ArrayList<>();
        String queryName = adminQuery.getName();
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        Date beginCreateTime = adminQuery.getBeginCreateTime();
        Date endCreateTime = adminQuery.getEndCreateTime();
        if (beginCreateTime != null && endCreateTime != null) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }
        sql += " LIMIT ?,?";
        int offset = (adminQuery.getPage() - 1) * adminQuery.getLimit();
        queryList.add(offset);
        queryList.add(adminQuery.getLimit());

        List<Admin> list = template.query(sql, new BeanPropertyRowMapper<>(Admin.class), queryList.toArray());
        return list;
    }

    @Override
    public int selectTotalCount(AdminQuery adminQuery) {
        String sql = "SELECT count(*) FROM admin WHERE 1=1";
        //构造一个集合存放所有的条件
        List<Object> queryList = new ArrayList<>();
        String queryName = adminQuery.getName();
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        Date beginCreateTime = adminQuery.getBeginCreateTime();
        Date endCreateTime = adminQuery.getEndCreateTime();
        if (beginCreateTime != null && endCreateTime != null) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }

        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void deleteById(int id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "DELETE FROM admin WHERE id=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            System.out.println(preparedStatement);
            int count = preparedStatement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(connection, preparedStatement, null);
        }
    }

    @Override
    public void add(Admin admin) {
        String sql = "INSERT INTO admin(name,password,role,image) VALUES (?,?,?,?)";
        template.update(sql, admin.getName(), admin.getPassword(), admin.getRole(), admin.getImage());
    }

    @Override
    public Admin selectById(int id) {
        String sql = "SELECT id,name,password,role,image,status,is_deleted,create_time,update_time FROM admin WHERE id=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Admin.class), id);
    }

    @Override
    public void update(Admin admin) {
        String sql = "UPDATE admin SET name=?,password=?,role=?,image=? WHERE id=?";
        template.update(sql, admin.getName(), admin.getPassword(), admin.getRole(),admin.getImage(), admin.getId());
    }

    @Override
    public void updateStatus(String id, int status) {
        String sql = "UPDATE admin SET status=? WHERE id=?";
        template.update(sql, status, id);
    }

    @Override
    public Admin login(String name, String password) {
        String sql = "SELECT id,name,password,role,image,status,is_deleted,create_time,update_time FROM admin WHERE name=? AND password=?";
        try {
            return template.queryForObject(sql, new BeanPropertyRowMapper<>(Admin.class), name, password);
        } catch (EmptyResultDataAccessException e) {
            e.printStackTrace();
            return null;
        }

    }
}
